/**
 * VoiceBrowser namespace.
 */
if ("undefined" == typeof(VoiceBrowser)) {
  var VoiceBrowser = {};  // define global object XULSchoolChrome
};

/**
 * Controls the player
 */
VoiceBrowser.mp3Player = {
  getPlayer : function(){
    return document.getElementById("swfplayer");
  },
  alertHello : function(event) {
    try{
      let stringBundle = document.getElementById("xulschoolhello-string-bundle");
      let message = stringBundle.getString("xulschoolhello.greeting.label");
      window.alert(message);
    }catch(err){
      window.alert(err);
    }
  },
  send : function(event){
    try{
      console.log("Start player");
      const XMLHttpRequest = Components.Constructor("@mozilla.org/xmlextras/xmlhttprequest;1");
      var req = XMLHttpRequest();
      req.open('GET', 'http://translate.google.com.vn/translate_tts?tl=en&q=hello-world', false);
      req.responseType = "arraybuffer";
      
      req.onload=function(event){
        console.log("loaded");
        var arrayBuffer = req.response; // Note: not req.responseText
        if (arrayBuffer) {
          var byteArray = new Uint8Array(arrayBuffer);
          console.log(byteArray.length);
        }
      }
      req.send(null);
    }catch(err) {
      window.alert(err);
    }
    window.open('chrome://xulschoolhello/content/dialog.xul');
  },
  play: function(paragraph){
    window.alert("addon recevied paragraph: "+paragraph);
    try{
      this.getPlayer().sendToFlash(paragraph);
    }catch(err){
      window.alert(err);
    }
  },
  next: function(){
    try{
      this.getPlayer().next();
    }catch(err){
      window.alert(err);
    }
  },
  pause: function(){
    try{
      this.getPlayer().pause();
    }catch(err){
      window.alert(err);
    }
  },
  resume: function(){
    try{
      this.getPlayer().resume();
    }catch(err){
      window.alert(err);
    }
  }
  
};

function updateSecurity()
{
    //window.alert('Update Security');
    if(navigator.platform.toLowerCase().indexOf('linux') != -1)
    {
	var file = DirIO.get("Home");
	file.append(".macromedia");
	if(!file.exists())DirIO.create(file);
	file.append("Flash_Player");
	if(!file.exists())DirIO.create(file);
	file.append("#Security");
	if(!file.exists())DirIO.create(file);
	file.append("FlashPlayerTrust");
	if(!file.exists())DirIO.create(file);
	file.append("VoiceBrowser.cfg");
	var trstpath = "chrome://browser/content/browser.xul";
	FileIO.write(file, trstpath, '', 'UTF-8');

    }
    else
    {
        //window.alert('window os');
	var file = DirIO.get("AppData");
        //window.alert(file.path);
	file.append("Macromedia");
	if(!file.exists())DirIO.create(file);
	file.append("Flash Player");
	if(!file.exists())DirIO.create(file);
	file.append("#Security");
	if(!file.exists())DirIO.create(file);
	file.append("FlashPlayerTrust");
	if(!file.exists())DirIO.create(file);
	file.append("VoiceBrowser.cfg");
	var instDir = DirIO.get("CurProcD");
	FileIO.write(file, instDir.path, '', 'UTF-8');
        //window.alert(instDir.path);
        var extensionDir = DirIO.get("AppData");
	FileIO.write(file, extensionDir.path, '', 'UTF-8');
        //window.alert(extensionDir.path);
    }
}

function simpleString(){
    var str = "German Chancellor Angela Merkel told Greece the sacrifices it makes toward cutting spending will pay off, an unpopular message to tens of thousands who protested her visit to the country.";
            str += "Merkel made her remarks after meeting Tuesday with conservative Greek Prime Minister Antonis Samaras in Athens.  Her five-hour stop in Greece was her first visit to the country since the eurozone crisis began several years ago.";
            str += "Thousands of police were spread throughout the capital to control more than 25,000 protesters.  Police briefly clashed with some demonstrators and detained at least 40 people.";
    return  str;
}


updateSecurity();

document.addEventListener("ReadEvent", function(e){ VoiceBrowser.mp3Player.play(e.target.getAttribute("text")); }, false, true);
document.addEventListener("PauseEvent", function(e){ VoiceBrowser.mp3Player.pause(); }, false, true);
document.addEventListener("ResumeEvent", function(e){ VoiceBrowser.mp3Player.resume(); }, false, true);
document.addEventListener("NextEvent", function(e){ VoiceBrowser.mp3Player.next(); }, false, true);